package com.yhp.web.userweb;

import com.yhp.pojo.Orders;
import com.yhp.service.userservice.CenterOrderService;
import com.yhp.utils.JSONResult;
import com.yhp.utils.PagedGridResult;
import com.yhp.vo.OrderStatusCountsVO;
import com.yhp.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "用户中心-个人订单",tags = {"用户中心我的订单相关接口"})
@RestController
@RequestMapping("/myorders")
public class MyOrderController extends BaseController {
  //  @Autowired
   // private  CenterOrderService centerOrderService;


    @PostMapping("/query")
    @ApiOperation(value = "个人订单查询",notes = "个人订单查询",httpMethod = "POST")
    public JSONResult search(
            @ApiParam(name = "userId",value = "用户id")
                    String userId,
            @ApiParam(name = "orderStatus",value = "订单状态")
                    String orderStatus,
            @ApiParam(name = "page",value = "页码值")
                    Integer page,
            @ApiParam(name = "pageSize",value = "每页显示条数")
                    Integer pageSize){

        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=COMMENT_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = centerOrderService.queryMyOrders(userId, orderStatus, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }

    //商家发货没有后端，所以这个接口仅仅用于模拟商家发货
    @ApiOperation(value="商家发货",notes = "商家发货",httpMethod = "GET")
    @GetMapping("/deliver")
    public JSONResult deliver(
            @ApiParam(name = "orderId",value = "订单id",required = true)
            String orderId){
        if(StringUtils.isBlank(orderId)){
            return JSONResult.errorMsg("订单id不允许为空");
        }
        centerOrderService.updateDeliverOrderStatus(orderId);

        return JSONResult.ok();
    }

    @ApiOperation(value="用户确认收货",notes = "用户确认收货",httpMethod = "POST")
    @PostMapping("/confirmReceive")
    public JSONResult confirmReceive(
            @ApiParam(name = "orderId",value = "订单id",required = true)
                    String orderId,
            @ApiParam(name = "userId",value = "用户id",required = true)
                    String userId){
        if(StringUtils.isBlank(orderId)||StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("订单id或userid不允许为空");
        }
        //1.先确认是否是自己的订单
        JSONResult jsonResult = checkUserOrder(userId, orderId);
        if(jsonResult.getStatus()!= HttpStatus.OK.value()){
            return jsonResult;
        }
        //2.更新订单状态
        boolean b = centerOrderService.updateOrderStatusReceived(orderId);
        if(!b){
            return JSONResult.errorMsg("订单确认收货失败");
        }
        return JSONResult.ok();
    }


    //删除订单
    @ApiOperation(value="用户删除订单",notes = "用户删除订单",httpMethod = "POST")
    @PostMapping("/delete")
    public JSONResult deleteOrder(
            @ApiParam(name = "orderId",value = "订单id",required = true)
                    String orderId,
            @ApiParam(name = "userId",value = "用户id",required = true)
                    String userId){
        if(StringUtils.isBlank(orderId)||StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("订单id或userid不允许为空");
        }
        //1.先确认是否是自己的订单
        JSONResult jsonResult = checkUserOrder(userId, orderId);
        if(jsonResult.getStatus()!= HttpStatus.OK.value()){
            return jsonResult;
        }
        //2.删除订单
        boolean b = centerOrderService.updateOrderStausDeleted(userId, orderId);
        if(!b){
            return JSONResult.errorMsg("订单删除失败");
        }

        return JSONResult.ok();
    }


    //查询订单的状态数量
    @ApiOperation(value="查询订单的状态数量",notes = "查询订单的状态数量",httpMethod = "POST")
    @PostMapping("/statusCounts")
    public JSONResult statusCounts(
            @ApiParam(name = "userId",value = "用户id",required = true)
                    String userId){
        if(StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("userid不允许为空");
        }

        OrderStatusCountsVO orderStatusCount = centerOrderService.getOrderStatusCount(userId);
        return JSONResult.ok(orderStatusCount);
    }

    //订单动向
    @ApiOperation(value="订单动向",notes = "订单动向",httpMethod = "POST")
    @PostMapping("/trend")
    public JSONResult trend(
            @ApiParam(name = "userId",value = "用户id",required = true)
                    String userId,
            @ApiParam(name = "page",value = "页码值")
                    Integer page,
            @ApiParam(name = "pageSize",value = "每页显示条数")
                    Integer pageSize){
        if(StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("userid不允许为空");
        }
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=COMMENT_PAGE_SIZE;
        }

        PagedGridResult orderTrend = centerOrderService.getOrderTrend(userId, page, pageSize);
        return JSONResult.ok(orderTrend);
    }
}
